package com.springboot_shiyan3.Controller;

import com.springboot_shiyan3.Bean.RegionStats;
import com.springboot_shiyan3.service.Restemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Controller
public class controller {

    private final Restemplate restemplate;

    public controller(Restemplate restemplate) {
        this.restemplate = restemplate;
    }

    @GetMapping("/")
    public String HomePage(Model model, String country){
        List<RegionStats> regionStatsSet = country == null ?
                restemplate.getALLRegionStatsSet() : restemplate.getSpecificRegionStatsSet(country);
        model.addAttribute("regionStatsSet",regionStatsSet);
        model.addAttribute("totalNumber",getTotalNumber(regionStatsSet));
        model.addAttribute("totalIncreaseNumber",getTotalIncreaseNumber(regionStatsSet));
        return "index";
    }

    public int getTotalNumber(List<RegionStats> regionStatsSet) {
        return regionStatsSet.parallelStream()
                .mapToInt(RegionStats::getLatestTotalCases)
                .sum();
    }

    public int getTotalIncreaseNumber(List<RegionStats> regionStatsSet) {
        return regionStatsSet.parallelStream()
                .mapToInt(RegionStats::getDifferFromPrevDEv)
                .sum();
    }


}
